﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Stock
{	public partial class ShiftDocLine : Form
	{	private	Util.ActionType actionType;
		private	bool checkRest;
//-------------------------------------------------------------------------------------------------
		public	DIProject.DataSets.Dictionary.PartiesRow PartyRow{get{return partyRow;}}
		private	DIProject.DataSets.Dictionary.PartiesRow partyRow = null;
//-------------------------------------------------------------------------------------------------
		public	int? Party
		{	get{return partyRow != null? (int?)partyRow.Id: null;}
			set
			{	if(actionType != Util.ActionType.Insert)
					party = value;
			}
		}
		private	int? party = null;

		public	string Manufacturer
		{	get{return textBoxManufacturer.Text;}
			set
			{	if(actionType != Util.ActionType.Insert)
					textBoxManufacturer.Text = value;
			}
		}

		public	int Qntty
		{	get{return (int)numericUpDownQntty.Value;}
			set
			{	if(actionType != Util.ActionType.Insert)
				{	sum						= value*Price;
					numericUpDownQntty.Value= value;
					textBoxSum.Text			= sum.ToString("N2");
				}
			}
		}

		public	decimal Price
		{	get{return price;}
			set
			{	if(actionType != Util.ActionType.Insert)
				{	price				= value;
					sum					= value*Qntty;
					textBoxPrice.Text	= price.ToString("N2");
					textBoxSum.Text		= sum.ToString("N2");
				}
			}
		}
		private	decimal price = 0m;

		public	decimal Sum{get{return sum;}}
		private	decimal sum = 0m;

		public	string Note
		{	get{return textBoxNote.Text;}
			set{textBoxNote.Text = value;}
		}
//-------------------------------------------------------------------------------------------------
		private void numericUpDownQntty_ValueChanged(object sender, EventArgs e)
		{	sum				= Price*(int)numericUpDownQntty.Value;
			textBoxSum.Text	= sum.ToString("N2");
		}
//-------------------------------------------------------------------------------------------------
		private void UpdateControls()
		{	if(partyRow != null)
			{	if(actionType == DIProject.Util.ActionType.Insert)
				{	price						= partyRow.RetailPrice;
					textBoxManufacturer.Text	= partyRow.ManufacturerName;
                    Qntty = partyRow.Qntty;	
               // if(Qntty > partyRow.Qntty)
						numericUpDownQntty.Value= partyRow.Qntty;
					numericUpDownQntty.Maximum	= partyRow.Qntty;
					sum							= price*Qntty;
					textBoxPrice.Text			= price.ToString("N2");
					textBoxSum.Text				= sum.ToString("N2");
				}
				else
				{	numericUpDownQntty.Maximum	= Qntty + partyRow.Qntty;
					buttonParty.Enabled			= false;
				}
				textBoxName.Text			= partyRow.Name;
				buttonAccept.Enabled		= true;
			}
			else
			{	numericUpDownQntty.Maximum	= int.MaxValue;
				buttonAccept.Enabled		= false;
			}
		}
//-------------------------------------------------------------------------------------------------
		public ShiftDocLine() : this(Util.ActionType.NoAction, false)
		{
		}
		public ShiftDocLine(Util.ActionType actionType, bool checkRest)
		{	InitializeComponent();
			numericUpDownQntty.Minimum = 1;
			numericUpDownQntty.Maximum = int.MaxValue;
			this.actionType	= actionType;
			this.checkRest	= checkRest;
		}
//-------------------------------------------------------------------------------------------------
		private void ShiftDocLine_Load(object sender, EventArgs e)
		{	if(actionType != DIProject.Util.ActionType.Insert)
			{	try
				{	partiesTableAdapter.Fill(dictionary.Parties, party, checkRest, null, null);
					if(dictionary.Parties.Count > 0)
					{	partyRow = dictionary.Parties[0];
						UpdateControls();
					}
				}
				catch(SqlException eq)
				{	MessageBox.Show(this, string.Format("{0} ({1}): {2}", eq.Procedure, eq.LineNumber, eq.Message), Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				catch(Exception ex)
				{	MessageBox.Show(this, ex.Message, Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				ActiveControl = numericUpDownQntty;
				Text = Properties.Resources.strUpdateDocLine;
			}
			else
				Text = Properties.Resources.strInsertDocLine;
		}
//-------------------------------------------------------------------------------------------------
		private void buttonParty_Click(object sender, EventArgs e)
		{	
            PartyChoice partyChoice = new PartyChoice(Party, true, null, null);
			if(partyChoice.ShowDialog(this) == DialogResult.OK)
			{	partyRow = partyChoice.CurrentRow;
				UpdateControls();
			}
		}
//-------------------------------------------------------------------------------------------------
	}
}